package com.baosight.sczx.zm.cc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cc.Qualitystandard;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.zm.cc.domain.ZMCCExamineCondition;
import com.baosight.sczx.zm.cc.domain.ZMCCExamineConditionDetail;
import com.baosight.sczx.zm.cc.domain.ZMCCQualitystandardItems;

/**
 * 质量标准化打分
 * <p>Title: ServiceZMCC05.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-28 上午11:21:22  严洪浩  Create
 *</pre>
 */
public class ServiceZMCC05 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS41</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-3上午05:31:26 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		StaticTools.setQueryParameters(inInfo, "fdYear", StaticTools.getYear(), true);//添加查询条件 年
		StaticTools.setQueryParameters(inInfo, "fdMonth", StaticTools.getIntMonth(), true);//添加查询条件 月
		
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(Department.getInstance().getConstructionUnit(true));//部门
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cs.department", "orgCode", "orgName", false));//部门  新增页面
		
		outInfo.addBlock(Engineering.getEngineering("cmes.cs.engineerings", "2", true));//工程
		outInfo.addBlock(Engineering.getEngineering("cmes.cs.engineering", "2", false));//工程 新增页面用
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:54 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		EiInfo outInfo = super.query(inInfo, "ZMCCExamineCondition.query", new ZMCCExamineCondition());
		
		return outInfo;
	}
	
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:48 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//校验名称、简称是否重复
		boolean validate = this.validate(inInfo, "ZMCCExamineCondition.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			String fdParentId = TablePk.getInstance().getNextValue("T_PL_EXAMINECONDITION");//主键
			inInfo.getBlock("result").setCell(0, "fdKeyId", fdParentId);
			inInfo.getBlock("result").setCell(0, "fdTrainingStarttime", Qualitystandard.getSumPoint());
			EiInfo outinfo = super.insert(inInfo, "ZMCCExamineCondition.insert", "result");
			
			inInfo.addBlock("resultDetails").addBlockMeta(new ZMCCExamineConditionDetail().eiMetadata);
			List _list = Qualitystandard.getQualityStandardItems();
			for(int i = 0; i < _list.size(); i++) {
				ZMCCQualitystandardItems _detail = (ZMCCQualitystandardItems)_list.get(i);
				String fdKeyId = TablePk.getInstance().getNextValue("T_PL_EXAMINECONDITIONDETAIL");
				String fdClassifyId = _detail.getFdClassifyId();
				String fdItemId = _detail.getFdKeyId();
				String fdReducePoint = "0";
				
				inInfo.getBlock("resultDetails").setCell(i, "fdKeyId", fdKeyId);
				inInfo.getBlock("resultDetails").setCell(i, "fdConditionId", fdParentId);
				inInfo.getBlock("resultDetails").setCell(i, "fdClassifyId", fdClassifyId);
				inInfo.getBlock("resultDetails").setCell(i, "fdItemId", fdItemId);
				inInfo.getBlock("resultDetails").setCell(i, "fdReducePoint", fdReducePoint);
			}
			
			super.insert(inInfo, "ZMCCExamineConditionDetail.insert", "resultDetails");
			return outinfo;
		}
	}

	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCC01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-28上午02:33:32 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		super.delete(inInfo, "ZMCCExamineConditionDetail.delete");//删除明细
		EiInfo _temp = super.delete(inInfo, "ZMCCExamineCondition.delete");//删除
		EiInfo outInfo = this.query(inInfo);
	    outInfo.setMsg(_temp.getMsg());
	    outInfo.setDetailMsg(_temp.getDetailMsg());

		return outInfo;
	}
	
	/**
	 * 重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-06-28下午04:18:21 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdOrgId = inInfo.getBlock("result").getCellStr(0, "fdOrgId");
		String fdDepartmentId = inInfo.getBlock("result").getCellStr(0, "fdDepartmentId");
		String fdYear = inInfo.getBlock("result").getCellStr(0, "fdYear");
		String fdMonth = inInfo.getBlock("result").getCellStr(0, "fdMonth");
		
		params.put("fdOrgId", fdOrgId);
		params.put("fdDepartmentId", fdDepartmentId);
		params.put("fdYear", fdYear);
		params.put("fdMonth", fdMonth);
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("提交失败，该部门" +fdYear+ "年 " +fdMonth+"月份，该工程的评分记录已经存在！");
			return true;
		}
		return false;
	}
}
